/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.sequence.ui.dialogs;

import java.util.HashSet;
import java.util.Iterator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.Event;
import org.eclipse.uml2.uml.ExecutionOccurrenceSpecification;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
import org.eclipse.uml2.uml.OccurrenceSpecification;

import com.cea.papyrus.core.PapyrusPlugin;

/**
 * 
 */
public class ChooseEventofLifeLineDialog extends Dialog {

	protected Lifeline lifeline;

	/**
	 * 
	 */
	protected Tree assocTree;
	protected Event theEvent;


	/**
	 * 
	 * 
	 * @param target 
	 * @param source 
	 * @param instanceSpecification 
	 * @param parent 
	 */
	public ChooseEventofLifeLineDialog (Shell parent, Lifeline lifeline){
		super(parent);
		this.lifeline=lifeline;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	protected Control createDialogArea(Composite parent){
		Composite comp=(Composite)super.createDialogArea(parent);
		assocTree = new Tree(comp, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);

		// this set will contain the classifiers classifying the source and target instance specifications.
		HashSet<OccurrenceSpecification> setMess = new HashSet<OccurrenceSpecification>();

		Iterator<InteractionFragment> fragIter = lifeline.getCoveredBys().iterator();
		while(fragIter.hasNext()){
			InteractionFragment itf=fragIter.next();
			if( itf instanceof OccurrenceSpecification){
				setMess.add(((OccurrenceSpecification)itf));
			}
		}

		// build tree item for each classifier of the instance specification owing this slot
		Iterator<OccurrenceSpecification> iter = setMess.iterator();
		while(iter.hasNext()){
			OccurrenceSpecification currentOccu = iter.next();
				TreeItem item = new TreeItem(assocTree, SWT.NONE);
				if(currentOccu instanceof  MessageOccurrenceSpecification){
					item.setText( "["+((MessageOccurrenceSpecification)currentOccu).getMessage().getName()+"] "+currentOccu.getEvent().getName());
				}
				else if(currentOccu instanceof  ExecutionOccurrenceSpecification){
					item.setText("["+((ExecutionOccurrenceSpecification)currentOccu).getExecution().getName()+"] "+currentOccu.getEvent().getName());
				}
				else{
					item.setText("["+(currentOccu).getName()+"] "+currentOccu.getEvent().getName());
				}
				item.setData(currentOccu);
				item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(currentOccu));
		}

		assocTree.setLayoutData(new GridData(300, 300));

		assocTree.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e){
				TreeItem[] selection = assocTree.getSelection();
				if(selection[0].getData() instanceof OccurrenceSpecification){
					theEvent = ((OccurrenceSpecification)selection[0].getData()).getEvent();
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		return comp;
	}


	/**
	 * Returns the elements to import.
	 * 
	 * @return 
	 */
	public Event getResult(){
		return theEvent;
	}



}